package com.shitou.shitouwarehouse.service.impl;

import com.alibaba.fastjson.JSON;
import com.shitou.shitoucommons.utils.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shitou.shitoucommons.utils.PageUtils;
import com.shitou.shitoucommons.utils.Query;

import com.shitou.shitouwarehouse.dao.ShAreaDao;
import com.shitou.shitouwarehouse.entity.ShAreaEntity;
import com.shitou.shitouwarehouse.service.ShAreaService;

import javax.annotation.Resource;


@Service("shAreaService")
public class ShAreaServiceImpl extends ServiceImpl<ShAreaDao, ShAreaEntity> implements ShAreaService {


    @Autowired
    @Qualifier("RedisTemplate")
    public RedisTemplate redisTemplate;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ShAreaEntity> page = this.page(
                new Query<ShAreaEntity>().getPage(params),
                new QueryWrapper<ShAreaEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 获得Sharea的树数据
     * @return
     */
    @Override
    public RespBean getTreeList() {
        String jsonString = (String) redisTemplate.opsForValue().get("allShaArea");
        if (jsonString != null && !jsonString.equals("")) {
            List<ShAreaEntity> shAreaEntityList = (List<ShAreaEntity>) JSON.parseArray(jsonString, ShAreaEntity.class);
            if ( shAreaEntityList != null && shAreaEntityList.size() > 0 ){
                return RespBean.success().put("data", shAreaEntityList);
            }
        }


        List<ShAreaEntity> list = this.list();
        List<ShAreaEntity> shAreaEntities = list.stream().filter( shAreaEntity -> shAreaEntity.getPid() == 0)
                .map(shAreaEntity -> {
                    shAreaEntity.setShAreaChildren(getShAreaChildren(shAreaEntity, list));
                    return shAreaEntity;
                }).collect(Collectors.toList());

        String json = JSON.toJSONString(shAreaEntities);
        redisTemplate.opsForValue().set("allShArea", json, 7, TimeUnit.DAYS);

        return RespBean.success().put("data", shAreaEntities);
    }


    /**
     * 递归查询
     * @param parent
     * @param areaEntityList
     * @return
     */
    public List<ShAreaEntity> getShAreaChildren(ShAreaEntity parent, List<ShAreaEntity> areaEntityList){
        if ( parent == null || areaEntityList == null) {
            return null;
        }
        List<ShAreaEntity> shAreaEntities = areaEntityList.stream()
                .filter( shAreaEntity -> shAreaEntity.getPid().equals(parent.getId()))
                .map(shAreaEntity -> {
                    shAreaEntity.setShAreaChildren(getShAreaChildren(shAreaEntity, areaEntityList));
                    return shAreaEntity;
                }).collect(Collectors.toList());
        return shAreaEntities;
    }




}
